package com.example.hms.awareness;

import android.annotation.TargetApi;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SwitchCompat;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    
    private String tag;
    private SensorUtil sensorUtil;
    private SwitchCompat openOrClose;
    private Button runInBackground;
    private Button exit;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tag = this.getResources().getString(R.string.app_name);

        // 检查是否已经授予权限
        if (!Settings.canDrawOverlays(this)) {
            // 若未授权则请求权限
            getOverlayPermission();
        }

        initViews();
        sensorUtil = new SensorUtil(getApplication());
    }
    
    private void initViews() {
        boolean enabled = Settings.canDrawOverlays(this);
        openOrClose = findViewById(R.id.open_or_close);
        openOrClose.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                    if (b) {
                        BarrierUtil.createBarrier(MainActivity.this, sensorUtil, openOrClose);
                    } else {
                        BarrierUtil.deleteBarrier(MainActivity.this, sensorUtil);
                    }
                }
            });
        openOrClose.setEnabled(enabled);
        runInBackground = findViewById(R.id.run_in_background);
        runInBackground.setEnabled(enabled);
        exit = findViewById(R.id.exit);
        exit.setEnabled(enabled);
    }
    
    // 请求悬浮窗权限
    @TargetApi(Build.VERSION_CODES.M)
    private void getOverlayPermission() {
        Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
        intent.setData(Uri.parse("package:" + getPackageName()));
        startActivityForResult(intent, 0);
    }
    
    @Override
    public void onBackPressed() {
        //此处写退向后台的处理
        moveTaskToBack(true);
    }
    
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.request_permission:
                getOverlayPermission();
                break;
            case R.id.run_in_background:
                moveTaskToBack(true);
                break;
            case R.id.exit:
                BarrierUtil.deleteBarrier(MainActivity.this, sensorUtil);
                finish();
                break;
        }
    }
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 0) {
            if (Settings.canDrawOverlays(this)) {
                openOrClose.setEnabled(true);
                runInBackground.setEnabled(true);
                exit.setEnabled(true);
            } else {
                Toast.makeText(this, "need overlay permission", Toast.LENGTH_SHORT).show();
            }
        }
    }
}